#ifndef WINDOW_H
#define WINDOW_H

#include <vector>
#include <memory>
#include <SDL/SDL.h>

#include "frontend-utils.h"


// basic window management for drawing things and directing SDL input: the
//  main UI will normally fill the screen, but there are also dialog boxes
//  or other temporary windows (like the map overlay)

// window classes must inherit from this
class Window
{
public:
	Window(const Point& p, const Size& s) : pos(p), size(s) {}
	Window() {}

	void setPos(const Point& p) {pos = p;}
	Point getPos() const {return pos;}
	void setSize(const Size& s) {size = s;}
	Size getSize() const {return size;}

	// test whether a point is within this window
	bool hitTest(const Point& pt);

	// inheritors must define this function
	// ...draws the contents of this window onto the provided surface, using
	//  our stored position and size
	virtual void draw(SDL_Surface *surface) = 0;

	// also need some virtual functions (but not pure virtual) for handling mouse
	//  and keyboard input

private:
	// in "screen" coordinates; i.e. relative to the top-left corner of the
	//  application's window (if windowed), or of the entire screen (if fullscreen)
	Point pos;
	// size of the window
	Size size;
	// ...note that the window pos/size do *not* have to fit within the actual "screen" area;
	//  WindowManager must not assume that they're entirely (or even partially) on-screen
};


// window manager class: contains a sorted list of all the currently visible windows, and handles
//  dispatching of drawing and input events to the windows
// a WindowManager is itself a Window, which allows window classes to break themselves down
//  into smaller windows if desired (for example, the map window might wish to use a WindowManager
//  to dispatch mouse input to the target areas around each visible player)
class WindowManager : public Window
{
public:
	// draw all the visible windows, from back to front
	// (may detect completely obscured windows and skip them)
	void draw(SDL_Surface *surface);

	// also need to handle mouse and keyboard input: for mouse input,
	//  find the window that was clicked on (if any) and dispatch the event; for
	//  keyboard input, just give it to the window with keyboard focus (if any)

private:
	std::vector<std::shared_ptr<Window> > windows;
	// also need to track keyboard focus somehow

	// find the topmost window that contains a point, or return NULL if no
	//  windows contain it
	std::shared_ptr<Window> findWindow(const Point& pt);
};


#endif // WINDOW_H